package cloudspace.vm.javassist;

import java.util.List;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.expr.Expr;


public class InsertAfter extends AbstractCommand
{

    private static boolean inSuperChain(
        CtClass declaringClass,
        String superName )
    {
        if ( declaringClass != null )
        {
            try
            {
                if ( declaringClass.getName().equals( superName ) )
                {
                    return true;
                }
                else
                {
                    return inSuperChain( declaringClass.getSuperclass(),
                        superName );
                }
            }
            catch ( NotFoundException e )
            {
                ;
            }
        }
        return false;
    }


    protected InsertAfter( List<String> argList )
        throws MalformedCommandException
    {
        super( argList );
    }


    public void translate( Expr call, boolean isSuper )
        throws MalformedCommandException,
        CannotCompileException
    {
        // TODO Auto-generated method stub

    }


    public String getReplacement()
    {
        return unParsedArgs.get( 4 ).substring( 1,
            unParsedArgs.get( 4 ).length() - 1 );
    }


    public void translate( CtMethod member, String expressionName )
        throws MalformedCommandException,
        CannotCompileException
    {
        if ( expressionName.equals( getSignature() )
            && inSuperChain( member.getDeclaringClass(), unParsedArgs.get( 3 ) ) )
        {
            // String replacement = getReplacement();
            member.insertAfter( getReplacement() );

        }
    }

}
